package XMl;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/*
以products.xml作为基础，实现客户购买的方法，输入商品号和购买数量，计算应付金额，如果是折扣商品，打七折；如果购买数量超过库存量，给出提示；商品不存在，给出提示；购买成功，扣除相应的数量。最后输出应付款的金额。
 */
public class _4 {
    public static void main(String[] args) throws Exception {
        Scanner input=new Scanner(System.in);
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read(new File("C:\\Users\\董\\IdeaProjects\\JAVA76\\高级特性\\src\\XMl\\products.xml"));
        Element rootElement = read.getRootElement();//获取根元素对象（根标签）
        List<Element> list = rootElement.elements();//获取根下的所有子节点

        System.out.print("输入商品号:");
        String a=input.next();
        System.out.print("输入购买数量:");
        int b=input.nextInt();

        Element product = rootElement.element("product");
        Attribute discount = product.attribute("discount");
        int ll=0;
        for (int i = 0; i < list.size(); i++) {
            ll++;
            if (list.get(i).elementText("itemno").equals(a)){
                int stockcount = Integer.parseInt(list.get(i).elementText("stockcount"));//库存
                if (stockcount>=b){
                    if (Objects.equals(discount.getText(), "true")){
                        System.out.println(list.get(i).elementText("price"));
//                        int price = Integer.parseInt(list.get(i).elementText("price"));
                        double price = Double.parseDouble(list.get(i).elementText("price"));
                        System.out.println((price*b*0.7));

                    }else {
//                        int price = Integer.parseInt(list.get(i).elementText("price"));
                        double price = Double.parseDouble(list.get(i).elementText("price"));
                        System.out.println((price*b));

                    }
                    product.element("stockcount").setText(""+(stockcount-b));
                    break;
                }else {
                    System.out.println("库存不足");
                    break;
                }
            }
        }
        if (ll==list.size()){
                System.out.println("没有这个商品");
        }


    }
}
